package org.example.demo.model;

import lombok.Data;
import org.example.demo.enums.ResultStatus;

@Data
public class Result<T> {
    private ResultStatus code;  //业务码
    private String errMsg;  //错误信息,如果业务成功,errmsg为空
    private T data;  //使用泛型T来适应各种返回类型

    public static <T> Result success(T data) {  //成功封装提取
        Result result = new Result<>();
        result.setCode(ResultStatus.SUCCESS);  //返回状态码
        result.setData(data);
        return result;
    }

    public static Result nologin() {  //没有数据传给前端,所以括号没有参数
        Result result = new Result<>();
        result.setCode(ResultStatus.NOLOGIN);  //返回状态码
        result.setErrMsg("用户未登录");
        return result;
    }

    public static Result fail(String msg){
        Result result = new Result<>();
        result.setCode(ResultStatus.FAIL);  //返回状态码
        result.setErrMsg(msg);
        return result;
    }

    public static Result fail(ResultStatus resultStatus,String msg){
        Result result = new Result<>();
        result.setCode(resultStatus);  //返回状态码
        result.setErrMsg(msg);
        return result;
    }
}
